//
//  CMDBTagsCollectionView.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/22.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let tagsCollectionViewID = "tagsCollectionViewID"

class CMDBTagsCollectionView: BaseCollectionView {
    
    var tags : [CMDBAtibuteTagBean] = [CMDBAtibuteTagBean](){
        
        didSet{
            
            reloadData()
        }
    }
    
    override func commitInitOwnProperties() {
        super.commitInitOwnProperties()
        
        backgroundColor = UIColor.clearColor()

        registerClass(CMDBTagCollectionCell.self, forCellWithReuseIdentifier: tagsCollectionViewID)
        
    }
}

//MARK: dataSource
extension CMDBTagsCollectionView {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tags.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(tagsCollectionViewID, forIndexPath: indexPath) as! CMDBTagCollectionCell
        
        cell.tagModel = tags[indexPath.item]
        
        return cell
    }
}

//MARK: delegate
extension CMDBTagsCollectionView{
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(2, 0, 0, 2)
    }
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        let tagModel = tags[indexPath.item]
        
        let size = tagModel.name.suitToSize(MaxSize, fontSize: 12, lineSpacing: 1)
        
        return CGSize(width: size.width * 2 * CMDB_Rate + 5, height: 30 * CMDB_Rate)
    }
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 5
    }
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 5
    }
    
}
